package www.admin.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import www.admin.model.GroupResource;
import www.admin.model.SysRole;
import www.admin.model.User;
import www.admin.mybatis.Criteria;
import www.admin.service.IGroupResourceService;
import www.admin.service.ISysRoleService;
import www.admin.service.IUserService;
import www.admin.utils.BaseController;
import www.admin.utils.EncryptUtil;
import www.admin.utils.JsonResult;
import www.admin.utils.Layui;
import www.admin.utils.MessageConstant;
import www.admin.utils.StringUtil;

/***
 * 后台帐号管理
 * @author Administrator
 *
 */
@Controller  
@RequestMapping("/user")  
public class UserController extends BaseController{
	
	@Autowired
	private IUserService service;
	
	@Autowired
	private IGroupResourceService groupResourceService;
	
	@Autowired
	private ISysRoleService sysRoleService;
	
	/**
	 * 跳转成员列表页面
	 * @return
	 */
	@RequestMapping("/showuser")
	public String showuser(ModelMap map){
		Criteria<SysRole> param = new Criteria<SysRole>();
		param.addParam("status", 0);
		map.put("list", sysRoleService.queryPageList(param));
		return "user/userList";
	}
	
	/**
	 * 获取菜单列表
	 * @return
	 */
	@RequestMapping("/querypage")
	@ResponseBody
	public Map<String, List<GroupResource>> queryPage(HttpServletRequest request){
		Map<String, List<GroupResource>> maps = null;
		if (!StringUtil.isNullOrBlank(request.getParameter("name"))) {
			SysRole role = sysRoleService.queryByRoleKey(getUserId(request));
			if (role != null) {
				Criteria<GroupResource> param = new Criteria<GroupResource>();
				param.setTitie(request.getParameter("name"));
				param.addParam("parent_resource_id", 0);
				List<String> resourceList = getResourceId(role.getResourceId());
				param.addParam("resourceIdList", resourceList);
				param.setList(resourceList);
				maps = groupResourceService.queryPage(param);
			}
		}
		return maps;
	}
	
	/**\
	 * 获取成员列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/querypagelist")
	@ResponseBody
	public Layui queryPageList(HttpServletRequest request){
		Criteria<User> param = new Criteria<User>();
		param.setStartIndex(Integer.parseInt(request.getParameter("page")));
		param.setPageSize(Integer.parseInt(request.getParameter("limit")));
		if (!StringUtil.isNullOrBlank(request.getParameter("userId"))) {
			param.addParam("userId", request.getParameter("userId"));
		}
		if (!StringUtil.isNullOrBlank(request.getParameter("roleId"))) {
			param.addParam("roleId", request.getParameter("roleId"));
		}
		return service.queryPage(param);
	}
	
	/**
	 * 跳转到添加成员页面
	 * @param map
	 * @param userId
	 * @return
	 */
	@RequestMapping("/dateil")
	public String dateil(ModelMap map, String userId, String show){
		map.put("show", show);
		Criteria<SysRole> param = new Criteria<SysRole>();
		param.addParam("status", 0);
		map.put("list", sysRoleService.queryPageList(param));
		if (!StringUtil.isNullOrBlank(userId)) {
			map.put("users", service.queryEntity(userId));
		}
		return "user/userAdd";
	}
	
	/**
	 * 添加成员
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/insert" , method = { RequestMethod.POST })
	public JsonResult insert(User user){
	  JsonResult jr = JsonResult.newInstance(); 
	  User users = service.queryEntity(user.getUserId());
	  if (users != null) {
		  return jr.declareFailure(MessageConstant.USER_TYPE_6); 
	  }
	  user.setCreateTime(new Date());
	  user.setEndTime(new Date());
	  user.setPassword(EncryptUtil.encodeToken(user.getPassword()));
	  String ret = service.insert(user);
	  if (StringUtil.isNullOrBlank(ret)) {
		  ret = insertOpera(user.getRecord()+user.getUserId());
	  }
	  return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
	
	/**
	 * 删除成员
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete")
	public JsonResult delete(String userId,String record){
		JsonResult jr = JsonResult.newInstance(); 
		String ret = service.deleteById(userId);
		if (StringUtil.isNullOrBlank(ret)) {
		   ret = insertOpera(record+userId);
	    }
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
	
	/**
	 * 修改成员
	 * @param userId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/update", method = { RequestMethod.POST })
	public JsonResult update(User user){
		JsonResult jr = JsonResult.newInstance(); 
		Criteria<User> param = new Criteria<User>();
		user.setPassword(EncryptUtil.encodeToken(user.getPassword()));
		user.setEndTime(new Date());
		param.setRecord(user);
		param.addParam("user_id", user.getUserId());
		String ret = service.update(param);
		if (StringUtil.isNullOrBlank(ret)) {
			ret = insertOpera(user.getRecord()+user.getUserId());
		}
		return ret == null ? jr.declareSuccess() : jr.declareFailure(ret); 
	}
	
}
